//
//  SinaOriginalView.m
//  oc微博
//
//  Created by 安陆与 on 16/1/22.
//  Copyright © 2016年 安陆与. All rights reserved.
//

#import "SinaOriginalView.h"
#import "SinaStatusViewModel.h"
#import "SinaStatusModel.h"
#import "user.h"
#import "UIImageView+WebCache.h"
#import "SinaPictureView.h"


// 只要添加了这个宏，就不用带mas_前缀
#define MAS_SHORTHAND
// 只要添加了这个宏，equalTo就等价于mas_equalTo
#define MAS_SHORTHAND_GLOBALS
//这个头文件一定要写在宏定义下面
#import "Masonry.h"

@interface SinaOriginalView()

@property (nonatomic,weak) UIImageView *icon;
@property (nonatomic,weak) UILabel *nameLabel;
//等级图片
@property (nonatomic,weak) UIImageView *levelPic;
//认证图片
@property (nonatomic,weak) UIImageView *verifitiedImageView;

//时间
@property (nonatomic,weak) UILabel *timeLabel;

//来源
@property (nonatomic,weak) UILabel *resouceLabel;

//正文
@property (nonatomic,weak) UILabel *textLabel;

//配图
@property (nonatomic, weak) SinaPictureView *pictureView;

//底部约束
@property (nonatomic, strong) MASConstraint *bottomConstraint;

@end

@implementation SinaOriginalView



-(void)setOriginalModel:(SinaStatusViewModel *)originalModel{
    
    
    _originalModel = originalModel;
    
    _nameLabel.text = originalModel.model.user.screen_name;
    
    [_icon sd_setImageWithURL:originalModel.profile_URL];
    
    
    
    _levelPic.image = originalModel.vipLevel_Imaeg;
    
    _verifitiedImageView.image = originalModel.verifited_Image;
    
    _textLabel.text = originalModel.model.text;
    
    _timeLabel.text = originalModel.createdat_string;
    
    _resouceLabel.text = originalModel.source_string;
    
    [self.bottomConstraint uninstall];
    

    
    if (originalModel.pic_urls.count > 0) {
        [self needsUpdateConstraints];
        
        [self updateConstraintsIfNeeded];
        //说明有配图
//        NSLog(@"%d",originalModel.pic_urls.count);
        [self updateConstraints:^(MASConstraintMaker *make) {
           self.bottomConstraint = make.bottom.equalTo(self.pictureView.bottom);
        }];
        
        self.pictureView.hidden = NO;
        
        self.pictureView.picture = originalModel.pic_urls;
    }else{
        [self needsUpdateConstraints];
        
        [self updateConstraintsIfNeeded];
    
        [self updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraint = make.bottom.equalTo(self.textLabel.bottom);
        }];
        
        self.pictureView.hidden = YES;
    }
    

}


-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    
    return self;
}


//添加控件以及约束
-(void)setupUI{
    
    CGFloat kMargin = 10.0;
    //头像
    UIImageView *icon = [[UIImageView alloc]init];
    
    [self addSubview:icon];
    
    self.icon = icon;
    
    [icon setImage:[UIImage imageNamed:@"avatar_default_big"]];
    
    [icon sizeToFit];
    
    icon.layer.masksToBounds = YES;
    
    icon.layer.cornerRadius = 20;
    
    icon.layer.borderColor = [UIColor yellowColor].CGColor;
    
    icon.layer.borderWidth = 2;
    
    //昵称文本框
    UILabel *nameLabel = [[UILabel alloc]init];
    
    [self addSubview:nameLabel];
    
    self.nameLabel = nameLabel;
    
    nameLabel.font = [UIFont systemFontOfSize:16];
    

    
    //等级图片
    UIImageView *levelPic = [[UIImageView alloc]init];
    
    [self addSubview:levelPic];
    
    self.levelPic = levelPic;
    

    
    //认证图片
    UIImageView *verifitiedImageView = [[UIImageView alloc]init];
    
    [self addSubview:verifitiedImageView];
    
    self.verifitiedImageView = verifitiedImageView;
    

    
    //时间标签
    UILabel *timeLabel = [[UILabel alloc]init];
    
    [self addSubview:timeLabel];
    
    self.timeLabel = timeLabel;
    
    timeLabel.font = [UIFont systemFontOfSize:15];
    

    
    //来源标签
    UILabel *sourceLabel = [[UILabel alloc]init];
    
    [self addSubview:sourceLabel];
    
    self.resouceLabel = sourceLabel;
    
    sourceLabel.font = [UIFont systemFontOfSize:15];
    
    
    
    //文本标签
    UILabel *textLabel = [[UILabel alloc]init];
    
    [self addSubview:textLabel];
    
    self.textLabel = textLabel;
    
    textLabel.font = [UIFont systemFontOfSize:15];
    
    textLabel.numberOfLines = 0;
    
    //这里是配图
    SinaPictureView *pictureView = [[SinaPictureView alloc]init];
    
    [self addSubview:pictureView];
    
    self.pictureView =pictureView;
    
    //配图的约束 顶部和 左边
    [pictureView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.textLabel.bottom);
        make.left.equalTo(self.left).offset(5);
        
//        make.width.equalTo(100);
//        make.height.equalTo(100);
    }];
    
    
    
    //头像约束
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.mas_left).offset(kMargin);
        make.top.equalTo(self.mas_top).offset(kMargin);
        make.width.mas_equalTo(40);
        make.height.equalTo(40);
    }];
    
    //昵称约束
    [nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.icon.right).offset(10);
        make.top.equalTo(self.top).offset(kMargin);
        
    }];

    
//    //等级图片约束
    [levelPic makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.nameLabel.right).offset(kMargin);
        
        make.centerY.equalTo(self.nameLabel.centerY);
    }];
    
    //认证约束
    [verifitiedImageView makeConstraints:^(MASConstraintMaker *make){
        make.centerY.equalTo(self.icon.bottom);
        make.centerX.equalTo(self.icon.right);
        
    }];

    //时间约束
    [timeLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.icon.right).offset(kMargin);
        
        make.top.equalTo(self.nameLabel.bottom).offset(kMargin/2);
    }];

    //来源约束
    [sourceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeLabel.right).offset(kMargin);
        make.centerY.equalTo(self.timeLabel.centerY);
    }];
    //文本约束
    [textLabel makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.left).offset(kMargin);
        make.top.equalTo(self.icon.bottom).offset(kMargin);
        
        make.width.equalTo([UIScreen mainScreen].bounds.size.width - 20);
//        make.bottom.equalTo(self.bottom);
    }];
    //底部的约束
//    [self makeConstraints:^(MASConstraintMaker *make) {
//       
//        
//    }];

}




@end
